﻿using DingTalkAgent.Model;
using DingTalkAgent.SqlData;
using Li.DingTalk.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DingTalkAgent.Web.Areas.mobile.Controllers
{
    public class CustomerController : mobileBaseController
    {
        T_CustomerRepository dal = new T_CustomerRepository();
        // GET: mobile/Customer
        public ActionResult Index()
        {
            return View();
        }

        #region 客户信息

        public ActionResult Customer()
        {
            T_CustomerDTO dto = new T_CustomerDTO();
            var ds = dal.GetCustomerList(1, 5, null);
            dto.rows = JsonHelpers.DataSetToList<T_Customer>(ds, 0);
            dto.page = 1;
            dto.pageSize = 5;
            dto.records = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            return View(dto);
        }

        public JsonResult GetCustomerList()
        {
            T_CustomerDTO dto = new T_CustomerDTO();
            int page = this.Parse<Int32>(Request, "page", 1);
            int rows = this.Parse<Int32>(Request, "rows", 10);
            string q = this.Parse<String>(Request, "q", null);
            if (Request.IsAjaxRequest())
            {
                var ds = dal.GetCustomerList(page, rows, q);
                dto.rows =JsonHelpers.DataSetToList<T_Customer>(ds, 0);
                dto.page = page;
                dto.pageSize = rows;
                dto.records = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return Json(dto, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddCustomer()
        {
            return View(new T_Customer());
        }

        public JsonResult GetSaleUserList()
        {
            List<Model.users> users = new List<Model.users>();
            users.Add(new Model.users { id = 1, name = "张三" });
            users.Add(new Model.users { id = 2, name = "李四" });
            users.Add(new Model.users { id = 3, name = "九筒" });
            users.Add(new Model.users { id = 4, name = "王麻子" });
            users.Add(new Model.users { id = 5, name = "师爷" });
            users.Add(new Model.users { id = 6, name = "老刘" });
            return Json(users, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(T_Customer mod)
        {
            var result = dal.Save(mod);
            return Json(new { status = result > 0, msg = result > 0 ? "添加成功" : "添加失败" }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 就医档案
        public ActionResult Archives(int customerId)
        {
            T_MedicalArchivesDTO dto = new T_MedicalArchivesDTO();
            var ds = dal.GetArchivesList(1, 5, customerId);
            dto.rows = JsonHelpers.DataSetToList<T_MedicalArchives>(ds, 0);
            dto.page = 1;
            dto.pageSize = 5;
            dto.records = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            ViewBag.UserId = customerId;
            return View(dto);
        }

        public ActionResult AddArchives(int customerId)
        {
            ViewBag.UserId = customerId;
            return View();
        }

        public JsonResult GetArchivesList()
        {
            T_MedicalArchivesDTO dto = new T_MedicalArchivesDTO();
            int page = this.Parse<Int32>(Request, "page", 1);
            int rows = this.Parse<Int32>(Request, "rows", 10);
            int customerId = this.Parse<Int32>(Request, "customerId", -1);
            if (Request.IsAjaxRequest())
            {
                var ds = dal.GetArchivesList(page, rows, customerId);
                dto.rows = JsonHelpers.DataSetToList<T_MedicalArchives>(ds, 0);
                dto.page = page;
                dto.pageSize = rows;
                dto.records = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return Json(dto, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SaveArchives(T_MedicalArchives mod)
        {
            var result = dal.SaveArchives(mod);
            return Json(new { status = result > 0, msg = result > 0 ? "添加成功" : "添加失败" }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 跟进记录
        public ActionResult FollowLog()
        {
            return View();
        }

        public ActionResult AddFollowLog()
        {
            return View();
        }



        #endregion

    }
}